package awt.awt05.game_01;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.io.File;

public class goBang {

    //private Frame fr=new Frame("五子棋");
    private JFrame fr=new JFrame("五子棋"); //关闭按钮就可以用了

    BufferedImage table;
    BufferedImage black;
    BufferedImage white;
    BufferedImage selected;

    //声明棋盘的宽高
    final int TableWidth=535;
    final int TableHeight=536;

    //棋盘横向和纵向分别可以下多少个子
    final int BOARD_SIZE=15;

    //每个棋子占用棋盘的宽度
    final int Rate=TableWidth/BOARD_SIZE;

    //记录棋子x,y方向上的偏移量
    final int X_Offset=5;
    final int Y_Offset=6;

    // 记录棋子 [i][j]的值为0没有棋子 1白棋 2黑棋
    int[][] board =new int[BOARD_SIZE][BOARD_SIZE];

    //声明红色选择框的索引坐标
    int selected_X=-1;
    int selected_Y=-1;


    //自定义类 继承canvas
    private class ChessBorad extends JPanel{ // awt为Canvas swing为JPanel
        @Override
        public void paint(Graphics g){
            //绘图

            //绘制棋盘
            g.drawImage(table,0,0,null);
            //绘制选择框
            if(selected_X>0&&selected_Y>0){
                g.drawImage(selected,selected_X*Rate+X_Offset,selected_Y*Rate+Y_Offset,null);
            }

            //绘制棋子
            for(int i=0;i<BOARD_SIZE;i++){
                for(int j=0;j<BOARD_SIZE;j++){
                    if(board[i][j]==2){
                         // 黑棋
                        g.drawImage(black,i*Rate+X_Offset,j*Rate+Y_Offset,null);
                    }else if(board[i][j]==1){
                        // 白棋
                        g.drawImage(white,i*Rate+X_Offset,j*Rate+Y_Offset,null);
                    }
                }
            }
        }
    }

    ChessBorad chessBorad=new ChessBorad();

    //记录当前棋子的颜色
    int board_type=0;
    //声明底部需要的组件
    Panel p=new Panel();
    Button whiteBth=new Button("白棋");
    Button blackBtn=new Button("黑棋");
    Button delBtn=new Button("删除");


    public void refreshBtnColor(Color whiteBtnColor,Color blackBtnColor,Color deleteBtnColor){
        whiteBth.setBackground(whiteBtnColor);
        blackBtn.setBackground(blackBtnColor);
        delBtn.setBackground(deleteBtnColor);
    }


    public void init() throws Exception{

        p.add(whiteBth);
        p.add(blackBtn);
        p.add(delBtn);

        table= ImageIO.read(new File("md_00\\img\\board.jpg"));
        white= ImageIO.read(new File("md_00\\img\\white.gif"));
        black= ImageIO.read(new File("md_00\\img\\black.gif"));
        selected= ImageIO.read(new File("md_00\\img\\selected.gif"));

        chessBorad.setPreferredSize(new Dimension(TableWidth,TableHeight));
        fr.add(chessBorad,BorderLayout.NORTH);
        fr.add(p);

        whiteBth.addActionListener(e->{
            // 修改棋子的标志为1
            board_type=1;
            //刷新按钮的颜色
            refreshBtnColor(Color.GREEN,Color.GRAY,Color.GRAY);
            System.out.println("白棋");

        });
        blackBtn.addActionListener(e->{
            // 修改棋子的标志为1
            board_type=2;
            //刷新按钮的颜色
            refreshBtnColor(Color.GRAY,Color.GREEN,Color.GRAY);

        });
        delBtn.addActionListener(e->{
            // 修改棋子的标志为1
            board_type=0;
            //刷新按钮的颜色
            refreshBtnColor(Color.GRAY,Color.GRAY,Color.GREEN);

        });


        chessBorad.addMouseMotionListener(new MouseMotionAdapter() {
            // 鼠标移动
            @Override
            public void mouseMoved(MouseEvent e) {
                System.out.println("x:"+e.getX());
                System.out.println("y:"+e.getY());
                System.out.println("=======分割线======");
                selected_X=(e.getX()-X_Offset)/Rate;
                selected_Y=(e.getY()-Y_Offset)/Rate;
                chessBorad.repaint();
            }
        });

        //鼠标点击动作
        chessBorad.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("点击");
                int xPos=(e.getX()-X_Offset)/Rate;
                int yPos=(e.getY()-Y_Offset)/Rate;
                board[xPos][yPos]=board_type; //添加棋子
                chessBorad.repaint();
            }

            @Override
            public void mouseExited(MouseEvent e) {
                System.out.println("移出");
                selected_X=-1;
                selected_Y=-1;
                chessBorad.repaint();
            }
        });

        fr.pack();
        fr.setVisible(true);
    }

    public static void main(String[] args) throws Exception{
        new goBang().init();
    }
}
